﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// 返回結果
/// </summary>
[DataContract]
class ArriveEstimable_Result
{
    [DataMember(Order = 0)]
    public string unit { set; get; }
    [DataMember(Order = 1)]
    public int number { set; get; }
}
public partial class Service_ArriveEstimable : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ArriveEstimable_Result result = new ArriveEstimable_Result();
        if (!string.IsNullOrEmpty(Request.QueryString["stid_arr"]))
        {
            using (MyDb.Db db = new MyDb.Db())
            {
                string rid = "0";
                string sid = "";
                string to_sid = Request.QueryString["stid_arr"];
                int min_seqno = 0;
                int max_seqno = 0;
                if (string.IsNullOrEmpty(Request.QueryString["stid"]))
                {
                    using (SqlDataReader rs = db.getList(string.Format("select a.stopid,b.seqno,a.routeid from busevent a left join stop b on b.id=a.StopID and b.GoBack=a.GoBack where a.busid='{0}' and a.GoBack={2}", Request.QueryString["bsid"], Request.QueryString["city"], Request.QueryString["gob"])))
                    {
                        if (rs.Read())
                        {
                            sid = rs["stopid"].ToString();
                            min_seqno = Convert.ToInt32(rs["seqno"]);
                            rid = rs["routeid"].ToString();
                        }
                        rs.Close();
                    }
                }
                else
                {
                    sid = Request.QueryString["stid"];
                    using (SqlDataReader rs = db.getList(string.Format("select seqno,routeid from stop a where a.id='{0}' and a.city='{1}' and a.GoBack={2}", Request.QueryString["stid"], Request.QueryString["city"], Request.QueryString["gob"])))
                    {
                        if (rs.Read())
                        {
                            min_seqno = Convert.ToInt32(rs["seqno"]);
                            rid = rs["routeid"].ToString();
                        }
                        rs.Close();
                    }
                }
                using (SqlDataReader rs = db.getList(string.Format("select seqno from stop a where a.id='{0}' and a.city='{1}' and a.GoBack={2}", to_sid, Request.QueryString["city"], Request.QueryString["gob"])))
                {
                    if (rs.Read())
                    {
                        max_seqno = Convert.ToInt32(rs["seqno"]);
                    }
                    rs.Close();
                }
                //找查站牌
                int bus_time = 0;
                int timeFrq = 0;
                int prev = 0;
                int itemtime = 0;
                
                using (DataTable dt = db.getDataTable(string.Format("select b.seqNo,a.Estimatetime,a.stopid from estimateTime a left join stop b on b.Id=a.StopID and b.goBack=a.GoBack where b.seqNo between {0} and {1} and a.GoBack={2} and b.City='{3}' and a.RouteID='{4}' order by b.seqNo",
                    min_seqno, max_seqno, Request.QueryString["gob"], Request.QueryString["city"], rid), "mess"))
                {
 
                    if (dt.Rows.Count >= 1)
                    {
                        bus_time = timeFrq = prev =0;// Convert.ToInt32(dt.Rows[0]["Estimatetime"]);
                        foreach (DataRow item in dt.Rows)
                        {
                            if (Convert.ToInt32(item["seqNo"]) > min_seqno+1)
                            {
                                itemtime = Convert.ToInt32(item["Estimatetime"]);
                                if (itemtime > prev)
                                {
                                    int timespan = itemtime - prev;
                                    timeFrq = timeFrq + timespan;
                                    prev = itemtime;
                                }
                                else
                                {
                                    timeFrq = timeFrq + itemtime;
                                    prev = itemtime;
                                }
                            }
                            
                        }
                    }
                }
                result.number = timeFrq/60;
                result.unit = "分鐘";                
            }
            
        }
        Response.Write(string.Format("{{\"estimable_time\":{0}}}", MyDb.JSON.stringify(result)));
    }
}
